package com.hengxinyongli.iam.web.controller;

import com.github.pagehelper.PageInfo;
import com.hengxinyongli.iam.entity.Department;
import com.hengxinyongli.iam.service.DepartmentService;
import com.hengxinyongli.iam.web.constant.Url;
import com.hengxinyongli.iam.web.vo.Responses;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.Set;

/**
 * @author magic
 * @version 1.0
 * Description DepartmentController
 * @date 2018/9/25/025 14:23
 */
@RestController
@Api(description = "部门相关api")
public class DepartmentController {

    @Autowired
    DepartmentService departmentService;

    @GetMapping(Url.DEPARTMENTS)
    @ApiOperation(value = "获取所有部门信息", notes = "获取所有部门信息")
    public Responses departments(@RequestParam(required = false) @ApiParam(value = "分页查询页号码") Integer pageNo,
                                 @RequestParam(required = false) @ApiParam(value = "分页查询页大小") Integer pageSize,
                                 @RequestParam(required = false) @ApiParam(value = "模糊查询部门名称") String departName,
                                 @RequestParam(required = false) @ApiParam(value = "部门状态")String status) {
        PageInfo pageInfo = departmentService.selectAllDepartments(pageNo, pageSize, departName, status);
        return Responses.success(pageInfo);
    }

    @GetMapping(Url.DEPARTMENTS_ONE)
    @ApiOperation(value = "获取指定ID部门信息", notes = "获取指定ID部门信息")
    @ApiResponses(
            @ApiResponse(code = 200,message = "操作成功",response = Department.class)
    )
    public Responses departmentsOne(@PathVariable @ApiParam(value = "指定的部门根据编号") String id) {
        Department department = departmentService.selectById(id);
        return Responses.success(department);
    }

    @PostMapping(Url.DEPARTMENTS)
    @ApiOperation(value = "创建新部门信息", notes = "创建新部门信息")
    @ApiResponses(
            @ApiResponse(code = 200,message = "操作成功",response = Department.class)
    )
    public Responses departmentsCreate(@RequestBody @Validated DepartmentRo departmentRo) {
        Department department = new Department();
        BeanUtils.copyProperties(departmentRo, department);
        departmentService.departmentsCreate(department, departmentRo.getPermits());
        return Responses.success(department);
    }

    @PutMapping(Url.DEPARTMENTS_ONE)
    @ApiOperation(value = "修改指定ID部门信息", notes = "修改指定ID部门信息")
    @ApiResponses(
            @ApiResponse(code = 200,message = "操作成功",response = Department.class)
    )
    public Responses departmentsUpdate(@PathVariable String id, @RequestBody @Validated DepartmentRo departmentRo) {
        Department department = new Department();
        BeanUtils.copyProperties(departmentRo, department);
        departmentService.departmentsUpdate(id, department,departmentRo.getPermits());
        return Responses.success(department);
    }

    @DeleteMapping(Url.DEPARTMENTS_ONE)
    @ApiOperation(value = "删除指定ID部门信息", notes = "删除获取指定ID部门信息")
    @ApiResponses(
            @ApiResponse(code = 200,message = "操作成功")
    )
    public Responses departmentsDelete(@PathVariable String id) {
        departmentService.departmentsDelete(id);
        return Responses.success(id);
    }

    @ApiModel
    public static class DepartmentRo {
        @ApiModelProperty(value = "部门名称")
        @NotEmpty(message = "部门名称不能为空")
        private String name;
        @ApiModelProperty(value = "编码")
        @NotEmpty(message = "部门编码不能为空")
        private String code;
        @ApiModelProperty(value = "状态")
        @NotEmpty(message = "状态不能为空")
        private String status;
        @ApiModelProperty(value = "上级部门")
        @NotEmpty(message = "上级部门不能为空")
        private String parentId;
        //@NotEmpty(message = "部门权限集合不能为空")
        @ApiModelProperty(value = "部门权限集合")
        private Set<String> permits;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        public String getParentId() {
            return parentId;
        }

        public void setParentId(String parentId) {
            this.parentId = parentId;
        }

        public Set<String> getPermits() {
            return permits;
        }

        public void setPermits(Set<String> permits) {
            this.permits = permits;
        }
    }

}

